package eight.test_8_18;

import java.util.Scanner;

public class TestMain {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int[][] arr = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = scanner.nextInt();
            }
        }
        int count = patrol(arr);
        System.out.println(count);
    }
    public static int patrol(int[][] block) {
        int rows = block.length;
        int cols = block[0].length;
        int row = 0;
        int col = 0;
        int direction = 0;
        int count = 1;
        block[row][col] = 2;
        while (true) {
            boolean tmp = true;
            if (direction == 0) {
                if(col + 1 < cols ) {
                    if(block[row][col + 1] == 0) {
                        col++;
                        count++;
                        block[row][col] = 2;
                        tmp = false;
                    }
                } else {
                    direction = (direction + 1) % 4;
                }
            } else if (direction == 1) {
                if (row + 1 < rows ) {
                    if (block[row + 1][col] == 0) {
                        row++;
                        count++;
                        block[row][col] = 2;
                        tmp = false;
                    }
                } else {
                    direction = (direction + 1) % 4;
                }
            } else if (direction == 2) {
                if(col - 1 >= 0 ) {
                    if (block[row][col - 1] == 0) {
                        col--;
                        count++;
                        block[row][col] = 2;
                    }
                    tmp = false;
                } else {
                    direction = (direction + 1) % 4;
                }
            } else if (direction == 3) {
                if(row - 1 >= 0 ) {
                    if (block[row - 1][col] == 0) {
                        row--;
                        count++;
                        block[row][col] = 2;
                    }
                    tmp = false;
                } else {
                    direction = (direction + 1) % 4;
                }
            }
            if(tmp) {
                break;
            }
        }
        return count;
    }
}
